{% from 'lib/interval.html' import interval_input -%}
{% extends 'lib/layout.html' %}

{% block scripts %}
{% endblock %}

{% block body -%}

<section class=viewer>
{% include "lib/greet.html" %}
</p>

{% if g.viewer.account.reason %}
<div class="banner warning" data-reason>
    {{ g.viewer.account.reason }}
    <form class='btn-group right' action='{{url_for("dismiss")}}' method='post' enctype='multipart/form-data'>
        <input class='btn dismiss' type='submit' value='Got it'>
        <input type='hidden' name='csrf-token' value='{{g.viewer.csrf_token}}'>
    </form>
    <div class='clearfix'></div>
</div>
{% endif %}


<div class='main-banner container'>
    {% set enabled_disabled = "enabled" if g.viewer.account.policy_enabled else "disabled" %}
    <div class="banner {{enabled_disabled}}">Forget is currently
    {% if g.viewer.account.policy_enabled -%}
        <b>enabled</b>
    {% else -%}
        disabled
    {% endif %}
        on your account.
    {% if g.viewer.account.policy_enabled -%}
        <form action='{{url_for("disable")}}' method='post' enctype='multipart/form-data'>
            <input type='submit' value='Disable'>
    {% else -%}
        <form action='{{url_for("enable")}}' method='post' enctype='multipart/form-data'>
            <input type='submit' value='Enable'>
    {% endif %}
            <input type='hidden' name='csrf-token' value='{{g.viewer.csrf_token}}'>
        </form>
    </div>
</div>
{% set post_count = g.viewer.account.post_count() %}
<p>Currently keeping track of <span id="post-count">{{ post_count }}</span> of your posts, roughly <span id="eligible-estimate">{{ g.viewer.account.estimate_eligible_for_delete() }}</span> of which currently match your expiration rules.</p>
{% if g.viewer.account.service == 'twitter'
        and g.viewer.account.reported_post_count
        and post_count < g.viewer.account.reported_post_count * 3/4
        and g.viewer.account.reported_post_count > 3200 -%}
<div class='banner warning'>Are we missing some of your posts? Try uploading a tweet archive below</div>
{% endif %}
</section>

<section id='settings-section'>
<h2 id="settings-title">Your expiration rules</h2>

{% if request.args.get('settings_saved') != None %}
<div class='banner success'>Rules saved successfully</div>
{% endif %}

{% if settings_error %}
<div class='banner error'>Something went wrong trying to save your settings. Please try again later.</div>
{% endif %}

<form name='settings' action='{{url_for("settings")}}' method='post' enctype='multipart/form-data'>
<p>Posts that are over
    {{interval_input(g.viewer.account, 'policy_keep_younger', scales)}}
    old and are not one of your
    <input type=number name=policy_keep_latest min=0 step=1 style='max-width:8ch' value={{g.viewer.account.policy_keep_latest}}>
    most recent posts will be considered for deletion
</p>
<p>…unless you
    <span class="radiostrip">
        <span class="choice">
        <input type=radio name=policy_keep_favourites value=keeponly id=policy_keep_favourites_keeponly {{ "checked" if g.viewer.account.policy_keep_favourites == 'keeponly' }}>
        <label for=policy_keep_favourites_keeponly>favourited them</label>
        </span>

        <span class="choice">
        <input type=radio name=policy_keep_favourites value=deleteonly id=policy_keep_favourites_deleteonly {{ "checked" if g.viewer.account.policy_keep_favourites == 'deleteonly' }}>
        <label for=policy_keep_favourites_deleteonly>have not favourited them</label>
        </span>

        <span class="choice">
        <input type=radio name=policy_keep_favourites value=none id=policy_keep_favourites_none {{ "checked" if g.viewer.account.policy_keep_favourites == 'none' }}>
        <label for=policy_keep_favourites_none>neither</label>
        </span>
    </span>
</p>
<p>…or unless they
    <span class="radiostrip">
        <span class="choice">
        <input type=radio name=policy_keep_media value=keeponly id=policy_keep_media_keeponly {{ "checked" if g.viewer.account.policy_keep_media == 'keeponly' }}>
        <label for=policy_keep_media_keeponly>have media</label>
        </span>

        <span class="choice">
        <input type=radio name=policy_keep_media value=deleteonly id=policy_keep_media_deleteonly {{ "checked" if g.viewer.account.policy_keep_media == 'deleteonly' }}>
        <label for=policy_keep_media_deleteonly>do not have media</label>
        </span>

        <span class="choice">
        <input type=radio name=policy_keep_media value=none id=policy_keep_media_none {{ "checked" if g.viewer.account.policy_keep_media == 'none' }}>
        <label for=policy_keep_media_none>neither</label>
    </span>
</p>
{% if g.viewer.account.service == 'mastodon' %}
<p>Keep direct messages:
    <span class="radiostrip">
        <span class="choice">
        <input type=radio name=policy_keep_direct value=true id=policy_keep_direct_true {{ "checked" if g.viewer.account.policy_keep_direct }}>
        <label for=policy_keep_direct_true>Yes</label>
        </span>

        <span class="choice">
        <input type=radio name=policy_keep_direct value=false id=policy_keep_direct_false {{ "checked" if not g.viewer.account.policy_keep_direct }}>
        <label for=policy_keep_direct_false>No</label>
        </span>
    </span>
</p>
{% endif %}
<p>Every
    {{interval_input(g.viewer.account, 'policy_delete_every', scales)}},
    one post matching these rules will be picked at random and deleted.
</p>
<input type=submit value='Save settings'>
<input type='hidden' name='csrf-token' value='{{g.viewer.csrf_token}}'>
</form>
</section>

<script type='text/json' data-viewer>
    {{ viewer_json|safe }}
</script>
<script async src="{{st('settings.js')}}"></script>

{% if g.viewer.account.service == 'twitter' %}
<section>

    <h2 id='tweet_archive_import'>Tweet archive import</h2>
    <p>
    Twitter's API only lets us access up to 3200 of your most recent tweets. If you have more tweets than that, you can request a tweet archive from
    <a href="https://twitter.com/settings/account#export_request">your account settings</a>,
    and upload it here.
    </p>

    {% if tweet_archive_failed %}
    <div class="banner error">The file you uploaded is not a valid tweet archive. No posts have been imported.</div>
    {% endif %}

    <div class="banner warning">Archive imports are temporarily disabled. The tweet archive format has been quietly dropped by Twitter and replaced by a more comprehensive data archive format. Support for this format will come soon.</div>

    <form action='{{url_for('upload_tweet_archive')}}' method='post' enctype='multipart/form-data' id='archive-form'>
        <input disabled type="file" name='file' accept='application/zip,.zip'><input disabled type="submit" value="Upload">
        <input type='hidden' name='csrf-token' value='{{g.viewer.csrf_token}}'>
    </form>

{% if g.viewer.account.twitter_archives %}
    <h3 id="recent_archives">Recent archives</h3>
    <ul>
    {% for archive in g.viewer.account.twitter_archives %}
        <li class="tweet_archive" data-status="{{archive.status()}}">{{archive.created_at.strftime('%Y-%m-%d %H:%M')}}<br>
            {% if archive.chunks is none %}
            Archive could not be opened
            {% else %}
            {{archive.chunks_successful}}/{{archive.chunks}} months imported, {{archive.chunks_failed}} failed</li>
            {% endif %}
    {% endfor %}
    </ul>
{% endif %}
</section>
{% endif %}

{% endblock %}
